<?
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=contoh.doc" );
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");
$konek= mysql_connect("localhost","root","");
mysql_select_db("agent");

$tpl_file = "form_perkara.doc"; //file template
if (file_exists($tpl_file)) {
$target = "contoh.doc"; //file yang tercipta saat proses

$f = fopen($tpl_file, "r+");
$isi = fread($f, filesize($tpl_file));
fclose($f);

$sql_data="SELECT * FROM agent";
$qry_data=mysql_query($sql_data, $konek) or die ("Gagal query");
$data = mysql_fetch_array($qry_data) or die ("Gagal mendapatkan data".mysql_error());

$isi = str_replace('nolap', $data['sup_id'], $isi);
$isi = str_replace('namapel', $data['name'], $isi);
$isi = str_replace('agama', $data['address'], $isi);
$isi = str_replace('alamatpel', $data['sex'], $isi);
$isi = str_replace('dataalamat', $data['O_id'], $isi);
//$isi = str_replace('datakelamin', $data['kelamin'], $isi);
//$isi = str_replace('datatgl', $data['tgl_lahir'], $isi); 
//echo $isi;

$f = fopen($target, "w+");
fwrite($f, $isi);
fclose($f);

echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=0;URL=$target>"; 

}
?>
